(function () {/**
 * Created by ChenJiangwei on 2017/05/02.
 */
var historyControl = function() {
    this.recordList = $('#record-list');
    this.searchInput = $('#search-input');
    this.searchBtn = $('#search-btn');
    this.resultNum = $('#village-num');
    this.clearSearchRecordBtn=$('#clearSearchRecord');
    this.maskControl=$('#mask');
    this.alertControl=$('#alert');
    this.waitDialog=$('#wait-dialog');
    this.dialog=$('#dialog');
};
historyControl.prototype = {
    //创建搜索历史列表
    buildSearchHistory:function(options){
        var dataList = options.dataList;
        var $targetSelector=$(options.targetSelector);
        var arrTemp=[];
        $.each(dataList,function(idx,ele){
            arrTemp.push('<li>'+ele+'</li>');
        });
        $targetSelector.html(arrTemp.join(''));
    },
    //清除搜索记录
    clearSearchRecord:function(key){
        var self=this;
        //显示弹框
        self.clearSearchRecordBtn.off('tap').on('tap',function(e){
            var $this=$(this);
            self.maskControl.removeClass('mask-hide').addClass("mask-show");
            self.alertControl.removeClass('mask-hide').addClass("mask-show");
            e.preventDefault();
            return false;
        });
        //点击取消按钮
         self.alertControl.children('.alert-cancel').off('tap').on('tap',function(e){
            self.maskControl.removeClass('mask-show').addClass("mask-hide");
            self.alertControl.removeClass('mask-show').addClass("mask-hide");
            e.preventDefault();
            return false;
        })
        //点击确定按钮
        self.alertControl.children('.alert-sure').off('tap').on('tap',function(e){
            self.recordList.children().remove();
            localStorage.removeItem(key)
            self.maskControl.removeClass('mask-show').addClass("mask-hide");
            self.alertControl.removeClass('mask-show').addClass("mask-hide");
            e.preventDefault();
            return false;
        })
    },
    //点击搜索记录回填到search-input框里
    rebackInput:function(){
        var self=this;
        self.recordList.find('li').off('tap').on('tap',function(e){
            var $this=$(this);
            self.searchInput.val($this.html());
            e.preventDefault();
            return false;
        })
    },
    maskShow:function(){
        var self=this;
        self.maskControl.show();
    },
    maskHide:function(){
        var self=this;
        self.maskControl.hide();
    },
    layerShow:function(){
        var self=this;
        self.waitDialog.removeClass('mask-hide').addClass('mask-show'); 
    },
    layerHide:function(){
        var self=this;
        self.waitDialog.removeClass('mask-show').addClass('mask-hide'); 
    },
    errDialog:function(msg){
        var self=this;
        self.dialog.removeClass('mask-hide').addClass('mask-show').html(msg);
        setTimeout(function() {
            self.dialog.removeClass('mask-show').addClass('mask-hide').html('');
        }, 500);
    },
    //添加缓存历史记录
    buildCacheHistory:function(key,searchVal){
        var historySearchRecordArr=[];
        historySearchRecordArr = common.getLocalData(key);
        //如果historySearchRecordArr不存在数据，给添加一个空数组；
        if(historySearchRecordArr===null||historySearchRecordArr.length===0){
            historySearchRecordArr = [];
            historySearchRecordArr.unshift(searchVal);
        }
        //保存的记录进行去重
        for(var i=0;i<historySearchRecordArr.length;i++){
            if(historySearchRecordArr[i]==searchVal){
                historySearchRecordArr.splice(i,1); 
            }
        }
        historySearchRecordArr.unshift(searchVal);
        //先获取缓存里面的搜索记录然后添加一条，如果缓存超过20条就删除之后的添加
        if(historySearchRecordArr.length>20){
            historySearchRecordArr=historySearchRecordArr.slice(0,20);
        }
        common.setLocalStorage(key,JSON.stringify(historySearchRecordArr));
    },
    buildVillageSearchResult:function(options){
        var dataList=options.dataList||[];
        var imgBasePath=options.imgBasePath||"./";
        var pageTargetType=options.pageTargetType||"_blank";
        var $loading=$(options.loadingSelector);
        var jumpLink=options.jumpLink;
        var arrTemp=[];
        var dataCount=0;
        var searchWord=options.searchWord;
        // console.log(dataList);
        $.each(dataList,function (idx, obj) {
            dataCount++;
            var villageName=obj['villageName']===""?"":obj['villageName'];//小区标题
            var villageSite="";//小区地址
                villageSite+=obj['cityAreaName']===""?"":obj['cityAreaName'];
                villageSite+=obj['commercialPlateName']===""?"":'-'+obj['commercialPlateName'];
            var houseNumber=obj['houseNumber']?obj['houseNumber']:0;//小区标题
            var tempDom = '<li><a class="clearfix" href="./'+jumpLink+'?villageId='+obj['villageName']+'" target="'+pageTargetType+'">'+
                                    '<div class="village-title">'+villageName.replace(searchWord,"<span>"+searchWord+"</span>")+'</div>'+
                                    '<div class="village-location">'+(villageSite.indexOf(searchWord)>-1?villageSite.replace(searchWord,"<span>"+searchWord+"</span>"):villageSite)+'</div>'+
                                    '<div class="village-data">'+houseNumber+'条数据</div>'+
                            '</a></li>';
            arrTemp.push(tempDom);
        });
         $loading.length>0?(dataList.length && dataList.length<10 || dataCount<10 ? $loading.text("已经没有更多信息了").show():$loading.text("").hide()):"无点击加载选择器";
        return arrTemp.join("");
    }
    
};

window.historyControl = new historyControl();
}).call(this);